import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:gsy_video_player/GSYVideoPlayerView.dart';
import 'package:uuid/uuid.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: HomePage(),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final String baseUrl = "http://116.62.164.143:9980/httpflv.rplay.";

  String sim = "";

  String videoPass = "";

  List<Widget> players = [];

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.max,
          children: [
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Row(
                children: [
                  Expanded(
                    child: TextField(
                      onChanged: (val) {
                        sim = val;
                      },
                      decoration: const InputDecoration(
                          contentPadding:
                              EdgeInsets.symmetric(vertical: 0, horizontal: 12),
                          hintText: '输入SIM号',
                          border: OutlineInputBorder()),
                    ),
                  ),
                  Expanded(
                    child: TextField(
                      onChanged: (val) {
                        videoPass = val;
                      },
                      decoration: const InputDecoration(
                          contentPadding:
                              EdgeInsets.symmetric(vertical: 0, horizontal: 12),
                          hintText: '通道数',
                          border: OutlineInputBorder()),
                    ),
                  ),
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: SizedBox(
                  width: double.infinity,
                  height: 45,
                  child: ElevatedButton(
                      onPressed: () {
                        getPlayers(size.width * 0.5);
                      },
                      child: const Text("播放"))),
            ),
            const SizedBox(height: 10),
            Wrap(
              children: players,
            )
            // SizedBox(
            //   height: 150,
            //   width: size.width * 0.5,
            //   child: const InfoWidget(
            //     tag: "video2",
            //     title: "通道2",
            //     dataSource:
            //         "https://fastly.jsdelivr.net/gh/chuzhixin/videos@master/video.mp4",
            //   ),
            // )
          ],
        ),
      ),
    );
  }

  getPlayers(width) {
    if (sim.isEmpty || videoPass.isEmpty) {
      return;
    }
    setState(() {
      players.clear();
      int size = int.parse(videoPass);
      for (int i = 1; i <= size; i++) {
        String uuid = Uuid().v4();
        String dataSource = baseUrl + uuid + '.admin._' + sim + ".$i." + "0.1";
        players.add(SizedBox(
          height: 150,
          width: width,
          child: GSYVideoPlayer(
            dataSource: dataSource,
            tag: uuid,
            title: "通道$i",
          ),
        ));
      }
    });
  }
}
